package com.fare.taluo.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.agent.AgentApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.vo.core.AgentEntityVO;
import com.fare.taluo.vo.core.SourceEntityVO;

@Controller("agentInfoAction")
@Scope("prototype")
public class AgentInfoAction extends BaseFdcAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1866904666567644357L;
	@Resource(name="agentApplication")
	private AgentApplication agentApplication;
	@Resource(name="sourceApplication")
	private SourceApplication sourceApplication;
	private AgentEntityVO vo;
	private List<SourceEntityVO> sourceLists;
//	private long dataId;
	private String flag;
	
	public String deal(){
		
		if("delete".equalsIgnoreCase(flag)){
			this.remove();
		}else{
			if(entityExist(""+vo.getAgentCode(),vo.getChannel())){
				flag = "existed";
				System.out.println("existed!");
				
				
			}else{
				try {
					agentApplication.saveOrUpdate(vo);
					/*returnPageURL = "/system/maintain/agentInfo.jsp";
					return "dispatcher";*/
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		}
		returnPageURL = "/system/maintain/agentInfo.jsp";
		return "dispatcher";
		
	}
	public void remove(){
		try {
			agentApplication.remove(vo.getId());
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		
	}
	
	private boolean entityExist(String agentCode,String channel){
		return agentApplication.entityExist(agentCode, channel);
	}
	
	public void getData(){
		try {
		List<AgentEntityVO> list = agentApplication.findAll();	
		StringBuffer resultBuffer = new StringBuffer();
		String jsonStr = null;
		
			jsonStr = com.fare.taluo.util.JsonUtil.toJson(list);
		
		resultBuffer.append("{\"Total\":"+list.size()+","+ "\"Rows\":"+jsonStr+"}");	
		String json = resultBuffer.toString();
		super.toJSONOut(json);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
	}
	
	public AgentEntityVO getVo() {
		return vo;
	}

	public void setVo(AgentEntityVO vo) {
		this.vo = vo;
	}

	public String toView(){
		sourceLists = sourceApplication.findAllSourceEntity();
		returnPageURL = "/system/maintain/agentInfo.jsp";
		return "dispatcher";
	}
	
	public List<SourceEntityVO> getSourceLists() {
		return sourceLists;
	}
	public void setSourceLists(List<SourceEntityVO> sourceLists) {
		this.sourceLists = sourceLists;
	}

	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	
}
